<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/authentification/User.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/language/language.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/mail/Mail_Sender.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/render/render_header_footer.php');



if (User::is_user_logged()){
    header('Location: index.php');
}


if ((isset($_POST['username'])) && (User::check_username_format($_POST['username']))
    && (isset($_POST['email'])) && (User::check_email_format($_POST['email']))
    && (isset($_POST['pass1'])) && (User::check_password_format($_POST['pass1']))){

    //test if username does not exists
    if (!User::user_exists($_POST['username'])){
        if (!User::email_exists($_POST['email'])){
            $new_user_id = User::register_user($_POST['username'], $_POST['pass1'], $_POST['email']);

            Mail_Sender::send_admin_new_user_register_mail($new_user_id);
            Mail_Sender::send_user_activation_mail($new_user_id);

            $content = Language::get_instance()->get_label('lb_user_created').'... '
                .Language::get_instance()->get_label('lb_redirect_in_5_sec');
            
            echo '<meta http-equiv="refresh" content="5;URL=index.php">';
        }
        else {
            echo Language::get_instance()->get_label('lb_email_already_exists');
        }
    }
    else {
        echo Language::get_instance()->get_label('lb_username_already_exists');
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="js/login_data_check.js"></script>
        <link rel="shortcut icon" type="images/x-icon" href="img/quest_jeton.png" />

        <style>
            @import url(css/base.css);
        </style>


        <script type="text/javascript">
            function validForm(){
                var error = 0;

                var username = document.getElementById('username').value;
                var email = document.getElementById('email').value;
                var pass1 = document.getElementById('pass1').value;
                var pass2 = document.getElementById('pass2').value;

                //Username must be set
                if (!username){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_username_must_be_set').'\'' ?>);
                }

                if ((!email) && (error == 0)){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_email_must_be_set').'\'' ?>);
                }

                if (((!pass1) || (!pass2)) && (error == 0)){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_password_must_be_set').'\'' ?>);
                }      

                //Username's length must be < 32
                if ((!checkUsernameFormat(username)) && (error == 0)){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_username_format').'\'' ?>);
                }

                if ((!checkEmailFormat(email)) && (error == 0)){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_email_format').'\'' ?>);
                }

                if ((((!checkPasswordFormat(pass1))) || (!checkPasswordFormat(pass2))) && (error == 0)){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_password_format').'\'' ?>);
                }

                if ((pass1 != pass2) && (error == 0)){
                    error = 1;
                    alert(<?php echo '\''.Language::get_instance()->get_label('lb_passwords_do_not_match').'\'' ?>);
                }

                //if no error redirect
                if (error == 0){
                    document.register_form.submit();
                }
            }
        </script>
    </head>
    <body>
        <center>
            <?php
                echo render_page_header().'</br>';
            ?>

            <fieldset>
                <h1 class="title">
                    <?php
                        echo Language::get_instance()->get_label('lb_register');
                    ?>
                </h1>
                <h3>
                    <?php
                        if (isset($content)){
                            echo $content.'<br/>';
                        }
                    ?>
                </h3>

                <form name ="register_form" method="post" accept-charset="utf-8">
                    <?php echo Language::get_instance()->get_label('lb_login_register') ?><br/><input type="text" name="username" id="username" value=""/><br/>
                    <?php echo Language::get_instance()->get_label('lb_email') ?><br/><input type="text" name="email" id="email" value=""/><br/>
                    <?php echo Language::get_instance()->get_label('lb_password_register') ?><br/><input type="password" name="pass1" id="pass1" value=""/><br/>
                    <?php echo Language::get_instance()->get_label('lb_password_again') ?><br/><input type="password" name="pass2" id="pass2" value=""/><br/>
                    <input type="button" value="<?php echo Language::get_instance()->get_label('lb_register'); ?>" onclick="validForm();" />
                </form>
            </fieldset>

            <?php
                echo render_page_footer();
            ?>
        </center>
    </body>
</html>
